<template>
  <!-- <div class="appo_print" style="width: 0px;height: 100px;visibility: hidden;" v-if="src"> -->
  <div class="appo_print" style="width: 50px;height: 100px;position: absolute;top:-30px;right: 10px;" v-if="src">
    <!-- <div @click="closeIframe()" class="printClose" v-if="appointmentLogIsUserC1Report && isShow && isOpenReportView">x</div> -->
    <iframe
      v-if="src"
      :src="src"
      ref="iframePrint"
      id="_iframe"
      @load="loaded"
      frameborder="0"
      width="100px"
      height="100px"></iframe>
  </div>
</template>

<script>
import { getparamtersettings } from '@/api/api'
const _url = window.g.APPOINT_URL ? window.g.APPOINT_URL : ''
export default {
  props: {
    loadingPrint: Boolean
  },
  data() {
    return {
      src: '',
      iframeWin: null,
      prints: [],
      printInfo: [],
      IS_ENABLE_BATCHPRINT: window.g.IS_ENABLE_BATCHPRINT,
      appointmentLogIsUserC1Report: false,
      lodopFuncs: false,
      isShow: false
    }
  },
  computed: {
    loading: {
      get() {
        return this.loadingPrint
      },
      set(val) {
        this.$emit('update:loadingPrint', val)
      }
    },
    // config配置
    appConfig() {
      return this.$store.state.settings.appConfig
    }
  },
  mounted() {
  },
  beforeDestroy() {
    window.removeEventListener('message', this.printLoad, false)
    clearInterval(window.printLoadingBox)
  },
  methods: {
    // closeIframe() {
    //   this.isShow = false
    //   // this.isOpenReportView = false
    // },
    getAppConfig(hospitalCode) {
      const userId = this.$store.state.user.id
      const params = {
        hospitalCode: hospitalCode,
        userId: userId
      }
      getparamtersettings(params).then(res => {
        if (res && res.code === 10000) {
          let configInfo = null
          if (res.content.hospitalParameterContent) {
            configInfo = JSON.parse(res.content.hospitalParameterContent)
            this.appointmentLogIsUserC1Report = configInfo.baseConfig.appointmentLogIsUserC1Report
            // console.error('但是', this.appointmentLogIsUserC1Report)
          }
          if (res.content.clinicalContent) {
            let clinicalContent = JSON.parse(res.content.clinicalContent || {})
            if (!(clinicalContent.lodopFuncs === '')) {
              this.lodopFuncs = clinicalContent.lodopFuncs
            }
          }
        }
      })
    },
    printLoad(event, callBack) {
      console.error('打印回调事件', event.data)
      this.loading = false
      if (event.data.type === 2) {
        this.$message.warning(event.data.msg)
        this.src = ''
      } else if (event.data.type === 'successPrint' || event.data.type == 1) {
        this.$parent.fetchData()
        this.src = ''
      }
    },
    print(data, callBack) {
      if (!_url) {
        this.$message.info('未配置打印调用的预约地址！')
        return
      }
      // console.log('data===>111111', data)
      // console.log('this.appointmentLogIsUserC1Report===>', this.appointmentLogIsUserC1Report)
      this.loading = true
      this.getAppConfig(data[0].hospitalCode)
      this.$nextTick(() => {
        if (data.length === 1 && data[0].tuiState === '退费单') {
          const { hospitalCode, operateCode, operateUser, accessionNo } = data[0]
          this.src = _url + '/his-print/index.html?hospitalCode=' + hospitalCode + '&dep=' + data[0].tuiState + '&accessNo=' + accessionNo + '&operateCode=' + operateCode + '&operateUser=' + operateUser + '&yeFlag=1'
          // console.log('this.src====>', this.src)
        } else {
          // 批量打印
          // if (this.IS_ENABLE_BATCHPRINT) {
          console.error('data[0]', data[0])
          const hospitalCode = data[0].hospitalCode
          const operateCode = data[0].operateCode
          const operateUser = data[0].operateUser
          const type = data[0].type
          let scheduledDep = []
          let accessionNo = []
          let ModalityType = []
          data.forEach(item => {
            scheduledDep.push(item.scheduledDep)
            accessionNo.push(item.accessionNo)
            ModalityType.push(item.scheduledModality) // 要传检查设备了
          })
          this.show = type == 'printReport'
          // let t = data['IsMergePrint'] ? '&printNoMerger=' + data['IsMergePrint'] : ''
          console.log('hospitalCode, operateCode, scheduledDep, operateUser, accessionNo===>2', hospitalCode, operateCode, scheduledDep.toString(), operateUser, accessionNo.toString())
          this.src = _url + '/his-print/index.html?hospitalCode=' + hospitalCode + '&dep=' + scheduledDep.toString().replace(/\,/g, '$,$') + '&accessNo=' + accessionNo.toString().replace(/\,/g, '$,$') + '&operateCode=' + operateCode + '&operateUser=' + operateUser + '&ModalityType=' + ModalityType.toString() + '&yeFlag=1' + '&type=' + type + '&printNoMerger=' + data['IsMergePrint'] + '&appointmentType=' + data[0].appointType
          console.error('this.src====>', this.src)
        }
        // if (this.loading == false && this.src == '') {
        window.printLoadingBox = setTimeout(() => {
          this.loading = false
          this.src = ''
        }, 180000)
        // }
      })
    },
    loaded() {
      window.addEventListener('message', this.printLoad, false)
    }
  }
}
</script>

<style lang="scss" scoped>
.ky-iframe{
  position: fixed;
  width: 80%;
  left: 10%;
  right: 10%;
  top: 0;
  height: 100vh;
  z-index: 9999;
}
.printClose {
	position: fixed;
	right: 50px;
	top: 50px;
	width: 50px;
	text-align: center;
	height: 50px;
	line-height: 40px;
	border-radius: 100%;
	font-size: 35px;
	font-weight: 0;
	background: rgba(0, 0, 0, .5);
	cursor: pointer;
	color: #fff;
}
</style>
